package master;

/**
 * @author chenyw
 * @date 2022/8/30 10:51
 */
public class M151reverseWords {
    public String reverseWords(String s) {
        s = s.trim();
        System.out.println(s);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        int index = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            if (c != ' ') {
                sb.insert(index, c);
                count++;
            } else {
                if (s.charAt(i + 1) != ' ') {
                    System.out.println("1");
                    sb.append(' ');
                    count++;
                    index = count;
                }
            }
        }
        return sb.toString();
    }
}
